﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Medical_Store_Billing.Component;
using System.Windows.Forms;
using Medical_Store_Billing.Object_Class;

namespace Medical_Store_Billing.Data_Access_Layer
{
    class DathangDAL
    {
        Dataservice m_dathangdal = new Dataservice();

        public DataTable Laychitiet(String name)
        {
            SqlCommand cmd = new SqlCommand("select mahang,soluong from chitietdondathang where madondathang='"+name+"'");
            m_dathangdal.Load(cmd);

            return m_dathangdal;
        }
        public DataTable LayDSdathang()
        {
            SqlCommand cmd = new SqlCommand("select * from dondathang where trangthai=0 order by madondathang ASC");
            m_dathangdal.Load(cmd);

            return m_dathangdal;
        }
        public DataTable Demdathang()
        {
            SqlCommand cmd = new SqlCommand("select count(*) from dondathang");
            m_dathangdal.Load(cmd);
            
            return m_dathangdal;
        }

        public DataTable Timdathang(string name)
        {
            SqlCommand cmd = new SqlCommand("Select * from dondathang where trangthai=0 and madondathang like N'%" + name + "%'");
            m_dathangdal.Load(cmd);
            return m_dathangdal;
        }

        public bool Themdathang(Dathangclass m_dh)
        {
            SqlCommand cmd = new SqlCommand("Insert into dondathang values(@madathang,@ngay,@makh,@manv,0)");
            cmd.Parameters.Add("madathang", SqlDbType.VarChar).Value = m_dh.madondat;
            cmd.Parameters.Add("ngay", SqlDbType.SmallDateTime).Value = m_dh.ngay;
            cmd.Parameters.Add("makh", SqlDbType.VarChar).Value = m_dh.makh;
            cmd.Parameters.Add("manv", SqlDbType.VarChar).Value = m_dh.manv;


            if (m_dathangdal.ExecuteNonQuery(cmd) > 0)
            {
                MessageBox.Show("Thêm Đơn đặt hàng thành công!");
                return true;
            }
            else return false;
        }
        public bool Suadathang(String madathang)
        {
            SqlCommand cmd = new SqlCommand("Update dondathang set trangthai=1 where madondathang='"+madathang+"'");
         
            if (m_dathangdal.ExecuteNonQuery(cmd) > 0)
            {
                return true;
            }
            else return false;
        }

        public bool Themchitietdathang(chitietdathangclass m_c)
        {
            SqlCommand cmd = new SqlCommand("Insert into chitietdondathang values(@madondh,@soluong,@mahang)");
            cmd.Parameters.Add("madondh", SqlDbType.VarChar).Value = m_c.madondat;
           
            cmd.Parameters.Add("soluong", SqlDbType.Int).Value = m_c.soluong;
            cmd.Parameters.Add("mahang", SqlDbType.VarChar).Value = m_c.mahang;


            if (m_dathangdal.ExecuteNonQuery(cmd) > 0)
            {
                return true;
            }
            else return false;
        }
    }
}
